<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机整数处理</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #F2E3D3;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 25px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }

        h1 {
            text-align: center;
            color: #333;
        }

        .output-area {
            height: 100px;
            width: 280px;
            border: 1px solid #ccc;
            padding: 10px;
            margin: 10px auto;
            overflow-y: scroll;
            background-color: white;
            border-radius: 5px;
        }

        .button-group {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 20px;
        }

        button {
            border-radius: 20px;
            width: 120px;
            height: 40px;
            border: none;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            font-weight: bold;
            margin: 2px 5px;
            padding: 5px 8px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        #resetBtn {
            background-color: #f44336;
        }

        #resetBtn:hover {
            background-color: #d32f2f;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>随机产生20个4位整数并从小到大进行排序</h1>

        <div class="output-area" id="output"></div>

        <div class="button-group">
            <button id="generateBtn">随机产生20个整数</button>
            <button id="filterBtn">找出能被5整除的整数</button>
            <button id="resetBtn">重置</button>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            let numbers = [];

            // 生成随机数按钮点击事件
            $('#generateBtn').click(function() {
                numbers = [];
                for (let i = 0; i < 20; i++) {
                    // 生成1000-9999之间的随机整数
                    numbers.push(Math.floor(Math.random() * 9000) + 1000);
                }

                // 排序数组
                numbers.sort((a, b) => a - b);

                // 显示结果
                $('#output').html(
                    '<strong>随机产生20个4位整数，分别如下：</strong><br>' +
                    numbers.join(', ') +
                    '<br><br><strong>从小到大排序后结果为：</strong><br>' +
                    numbers.join(', ')
                );
            });

            // 筛选能被5整除的数按钮点击事件
            $('#filterBtn').click(function() {
                if (numbers.length === 0) {
                    alert('请先生成随机数！');
                    return;
                }

                const divisibleBy5 = numbers.filter(num => num % 5 === 0);

                let output = $('#output').html();
                output += '<br><br><strong>能被5整除的整数有：</strong><br>' +
                          (divisibleBy5.length > 0 ? divisibleBy5.join(', ') : '无');

                $('#output').html(output);
            });

            // 重置按钮点击事件
            $('#resetBtn').click(function() {
                numbers = [];
                $('#output').empty();
            });
        });
    </script>
</body>
</html>
